package com.dc.contoller;
import com.alibaba.fastjson.JSON;
import com.dc.base.model.BaseModel;


import com.dc.service.UserService;
import com.dc.service.impl.UserServiceImpl;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * Created by Acer on 2019/9/28.
 */

@WebServlet(name="UserService",urlPatterns = {"/selectUser.do","/deleteUser.do","/addUser.do","/updateUser.do","/selectUserid.do"})
public class UserContoller extends HttpServlet {
    UserService userService=new UserServiceImpl();
    BaseModel baseModel=new BaseModel();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url=req.getRequestURI();
        url=url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
        Method method =null;
        try{
            method = getClass().getDeclaredMethod(url,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,req,resp);
        }catch (Exception e){
            System.out.println("java反射失败");
            e.printStackTrace();
        }
    }


    public  void selectUser(HttpServletRequest req, HttpServletResponse resp) throws  Exception{
        try {
            req.setCharacterEncoding("utf-8");
            //获取用户传入的参数
            baseModel=userService.selectUser(null);
            String baseModelStr= JSON.toJSONString(baseModel);
            System.out.println(baseModelStr);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json;charset=utf-8");

            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();

        }catch (Exception e){
            System.out.println("查询失败");
            e.printStackTrace();
        }
    }
    public void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws  Exception{

        try{
            String id=req.getParameter("id");
            System.out.println(id);
            req.setCharacterEncoding("utf-8");
            //获取页面id
            baseModel=userService.deleteUser(id);
            String baseModelStr=JSON.toJSONString(baseModel);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json;charset=utf-8");

            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();

        }catch (Exception e){
            System.out.println("删除数据失败");
            e.printStackTrace();
        }


    }

    public void addUser(HttpServletRequest req,HttpServletResponse resp)throws  Exception{
        try{
            Object[]  params = new Object[11];
            req.setCharacterEncoding("utf-8");
            params[0]=req.getParameter("user_name");
            params[1]=req.getParameter("user_trueName");
            params[2]=req.getParameter("user_cardId");
            params[3]=req.getParameter("user_email");
            params[4]=req.getParameter("user_telephone");
            params[5]=req.getParameter("user_qqId");
            params[6]=req.getParameter("user_pwd");
            params[7]=new Date();
            params[8]=req.getParameter("question");
            params[9]=req.getParameter("answer");
            params[10]=req.getParameter("state");
            baseModel=userService.addUser(params);
            String baseModelStr=JSON.toJSONString(baseModel);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();


        }catch (Exception e){
            e.printStackTrace();
            System.out.println("新增失败");
        }

    }
    public void updateUser(HttpServletRequest req,HttpServletResponse resp) throws  Exception{
        try{
            Object[]  params = new Object[12];
            req.setCharacterEncoding("utf-8");
            params[0]=req.getParameter("user_name");
            params[1]=req.getParameter("user_trueName");
            params[2]=req.getParameter("user_cardId");
            params[3]=req.getParameter("user_email");
            params[4]=req.getParameter("user_telephone");
            params[5]=req.getParameter("user_qqId");
            params[6]=req.getParameter("user_pwd");
            params[7]=new Date();
            params[8]=req.getParameter("question");
            params[9]=req.getParameter("answer");
            params[10]=1;
            params[11]=req.getParameter("user_id");
            baseModel=userService.updateUser(params);
            String baseModelStr=JSON.toJSONString(baseModel);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();


        }catch (Exception e){
            e.printStackTrace();
            System.out.println("新增失败");
        }

    }
    public void selectUserid(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        try {
            resp.setCharacterEncoding("utf-8");
            int id= Integer.valueOf(req.getParameter("id"));
            baseModel=userService.selectUserid(id);
            String baseModelStr=JSON.toJSONString(baseModel);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();

        }catch (Exception e){
            System.out.println("查询失败");
            e.printStackTrace();
        }


    }
}

